﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using QingzimaLogicModel;
using Jacaranda.Model;
using QingzimaDB;

namespace QingzimaScenario
{
    public class XinwenScenario
    {
        public static XinwenLM HuoquByID(int id)
        {
            return ConvertToLM(Database.SearchOne<XinwenDB>(new AbstractProperty("ID") == id));
        }

        public static XinwenLM HuoquByKey(string key)
        {
            return ConvertToLM(Database.SearchOne<XinwenDB>(new AbstractProperty("Key") == key));
        }

        public static List<XinwenLM> HuoquModels()
        {
            List<XinwenLM> tempLMList = new List<XinwenLM>();
            foreach (XinwenDB dbModel in Database.Select<XinwenDB>("Select * From XinwenDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static IEnumerable<XinwenLM> GetXinwenList(int pageSize, int pageIndex)
        {
            return Database.Search<XinwenDB>(new AbstractProperty("ID") != null, new SortCriteria().OrderByDesc("CreatedTime"), new SearchPageCriteria { PageIndex = pageIndex, PageSize = pageSize }).Select<XinwenDB, XinwenLM>(a => new XinwenLM(a));
        }

        public static List<XinwenLM> HuoquModelsByCreatedTime(int shuliang)
        {
            List<XinwenLM> tempLMList = new List<XinwenLM>();
            foreach (XinwenDB dbModel in Database.Select<XinwenDB>("Select top " + shuliang + " * From XinwenDB Order By CreatedTime Desc"))
            {
                tempLMList.Add(ConvertToLM(dbModel));
            }
            return tempLMList;
        }

        public static void Tianjia(XinwenLM xinwenLM)
        {
            xinwenLM.Save();
        }

        public static void Shanchu(string key)
        {
            Database.Delete<XinwenDB>(new AbstractProperty("Key") == key);
        }

        private static XinwenLM ConvertToLM(XinwenDB dbModel)
        {
            return dbModel == null ? null : new XinwenLM(dbModel);
        }

        public static int GetCount()
        {
            //throw new NotImplementedException();

            return Convert.ToInt32(Database.Count<XinwenDB>());
        }
    }
}
